/**
 *  Copyright (C) 2008-2013  Telosys project org. ( http://www.telosys.org/ )
 *
 *  Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, Version 3.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *          http://www.gnu.org/licenses/lgpl.html
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.telosys.persistence.impl;

import java.sql.Connection;
import java.sql.SQLException;

import org.telosys.common.TelosysRuntimeException;
import org.telosys.persistence.PersistenceTransaction;

public class PersistenceTransactionImpl implements PersistenceTransaction
{
	private final static String MSG_NOT_ACTIVE     = "Transaction is not currently active" ;
	private final static String MSG_ALREADY_ACTIVE = "Transaction already active" ;
	
	private Connection connection = null;

	private boolean    transactionInProgress = false ;
	
	public PersistenceTransactionImpl(Connection connection) {
		super();
		if ( null == connection ) {
			throw new TelosysRuntimeException("Cannot create transaction (connection is null)");
		}
		this.connection = connection;
		this.transactionInProgress = false ;
	}

	// @Override
	public void begin() 
	{
		if ( this.transactionInProgress ) {
			//--- Attempting to start a new transaction while a transaction is active 
			throw new IllegalStateException(MSG_ALREADY_ACTIVE);
		}
		
		try {
			if ( connection.isClosed() ) {
				throw new TelosysRuntimeException("Cannot begin transaction (connection is closed)");
			}
			//--- Beginning of transaction => remove AUTO-COMMIT mode
			connection.setAutoCommit(false);
		} catch (SQLException e) {
			throw new TelosysRuntimeException("Cannot begin transaction (SQLException)", e);
		}
		this.transactionInProgress = true ;
	}

	// @Override
	public void commit() 
	{
		if ( this.transactionInProgress != true ) {
			//--- Attempting to commit an inactive transaction 
			throw new IllegalStateException(MSG_NOT_ACTIVE);
		}
		
		try {
			connection.commit();
			//--- End of transaction => restore AUTO-COMMIT mode
			connection.setAutoCommit(true);
			//--- End of transaction => nothing in progress
			this.transactionInProgress = false ;
		} catch (SQLException e) {
			throw new TelosysRuntimeException("Cannot commit transaction (SQLException)", e);
		}		
	}

	// @Override
	public void rollback() 
	{
		if ( this.transactionInProgress != true ) {
			//--- Attempting to rollback an inactive transaction 
			throw new IllegalStateException(MSG_NOT_ACTIVE);
		}
		
		try {
			connection.rollback();
			//--- End of transaction => restore AUTO-COMMIT mode
			connection.setAutoCommit(true);
			//--- End of transaction => nothing in progress
			this.transactionInProgress = false ;
		} catch (SQLException e) {
			throw new TelosysRuntimeException("Cannot commit transaction (SQLException)", e);
		}		
	}

	// @Override
	public boolean isActive() 
	{
		return this.transactionInProgress ;
	}

}
